var express = require('express');
var router = express.Router();

/* 获取 */
router.get('/', function (req, res, next) {
	var limit = req.query.limit;
	var skip = req.query.skip;
	
	var search = {};
	
	if (req.query.type) {
		search.type = req.query.type;
	}
	if (req.query.payer) {
		search.payer = req.query.payer;
	}
	if (req.query.payee) {
		search.payee = req.query.payee;
	}
	if (req.query.moneyStart) {
		search.money = {
			'$gte' : req.query.moneyStart
		};
	}
	if (req.query.moneyEnd) {
		search.money = {
			'$lt' : req.query.moneyEnd
		};
	}
	if (req.query.status) {
		search.status = req.query.status;
	}
	if (req.query.orders) {
		if (Array.isArray(req.query.orders)) {
			search.orders = {
				$in : req.query.orders
			};
		} else {
			search.orders = req.query.orders;
		}
	}
	if (req.query.createTimeStart) {
		search.createTime = {
			'$gte' : req.query.createTimeStart
		};
	}
	if (req.query.createTimeEnd) {
		search.createTime = {
			'$lt' : req.query.createTimeEnd
		};
	}
	if (req.query.payTimeStart) {
		search.payTime = {
			'$gte' : req.query.payTimeStart
		};
	}
	if (req.query.payTimeEnd) {
		search.payTime = {
			'$lt' : req.query.payTimeEnd
		};
	}
	if (req.query.confirmTimeStart) {
		search.confirmTime = {
			'$gte' : req.query.confirmTimeStart
		};
	}
	if (req.query.confirmTimeEnd) {
		search.confirmTime = {
			'$lt' : req.query.confirmTimeEnd
		};
	}
	if (req.query.cancelTimeStart) {
		search.cancelTime = {
			'$gte' : req.query.cancelTimeStart
		};
	}
	if (req.query.cancelTimeEnd) {
		search.cancelTime = {
			'$lt' : req.query.cancelTimeEnd
		};
	}
	
	Db.Transaction.count(search, function(err, count) {
		if (err) {
			res.send(500, err);
			return;
		}
	
		Db.Transaction.find(search, {
			limit : limit,
			skip : skip
		}, function(err, transactions) {
			if (err) {
				res.send(500, err);
				return;
			}
			
			res.send({
				count : count,
				list : transactions
			});
		});
	});
});
router.get('/:id', function (req, res, next) {
	var id = req.params.id;
	Db.Transaction.findById(id, function(err, transaction) {
		if (err) {
			res.send(500, err);
			return;
		}
		
		if (transaction) {
			res.send(transaction);
		} else {
			res.send(404);
		}
	});
});

/* 创建 */
router.post('/', function (req, res, next) {
	var account = req.acct;
	var transaction = new Db.Transaction(req.body);
	
	var err = transaction.validateSync();
	if (err) {
		res.send(500, err);
		return;
	}
	
	// 加入操作记录
	transaction.actionHistories.push({
		action : '创建',
		accountType : account.type,
		accountId : account._id
	});
	
	transaction.status = '等待支付';
	
	transaction.save(function(err) {
		if (err) {
			res.send(500, err);
			return;
		}
		
		res.send(transaction);
	});
});

/* 支付 */
router.put('/:id/payTime', function(req, res, next) {
	var account = req.acct;
	var id = req.params.id;
	Db.Transaction.findById(id, function(err, transaction) {
		if (err) {
			res.send(500, err);
			return;
		}
		
		if (transaction) {
			if (transaction.status != '等待支付') {
				transaction.invalidate('status', '当前状态无法进行该操作。', '支付中');
				var err = transaction.validateSync();
				if (err) {
					res.send(400, err);
					return;
				}
			}
			
			transaction.payTime = new Date();
			transaction.status = '支付中';
			transaction.actionHistories.push({
				action : '支付',
				accountType : account.type,
				accountId : account._id
			});
			
			transaction.save(function(err) {
				if (err) {
					res.send(500, err);
					return;
				}
				
				res.send(transaction);
			});
		} else {
			res.send(404);
		}
	});
});

/* 支付确认 */
router.put('/:id/confirmTime', function(req, res, next) {
	var account = req.acct;
	var id = req.params.id;
	Db.Transaction.findById(id, function(err, transaction) {
		if (err) {
			res.send(500, err);
			return;
		}
		
		if (transaction) {
			if (transaction.status != '支付中') {
				transaction.invalidate('status', '当前状态无法进行该操作。', '支付成功');
				var err = transaction.validateSync();
				if (err) {
					res.send(400, err);
					return;
				}
			}
			transaction.confirmTime = new Date();
			transaction.status = '支付成功';
			transaction.actionHistories.push({
				action : '支付确认',
				accountType : account.type,
				accountId : account._id
			});
			
			transaction.save(function(err) {
				if (err) {
					res.send(500, err);
					return;
				}
				
				res.send(transaction);
			});
		} else {
			res.send(404);
		}
	});
});

/* 取消支付 */
router.put('/:id/startTime', function(req, res, next) {
	var account = req.acct;
	var id = req.params.id;
	Db.Transaction.findById(id, function(err, transaction) {
		if (err) {
			res.send(500, err);
			return;
		}
		
		if (transaction) {
			if (transaction.status != '等待支付') {
				transaction.invalidate('status', '当前状态无法进行该操作。', '支付取消');
				var err = transaction.validateSync();
				if (err) {
					res.send(400, err);
					return;
				}
			}
			transaction.startTime = new Date();
			transaction.status = '支付取消';
			transaction.actionHistories.push({
				action : '取消支付',
				accountType : account.type,
				accountId : account._id
			});
			
			transaction.save(function(err) {
				if (err) {
					res.send(500, err);
					return;
				}
				
				res.send(transaction);
			});
		} else {
			res.send(404);
		}
	});
});

module.exports = router;